
#include "config.h"
#include "inireader/INIReader.h"
//#include "base.h"
#include "file_opera.h"
#include "file_util.h"
//#include "base/json_key.h"
#include <memory>
#include <iostream>
#include <fstream>

namespace jouav
{
#define CONFIG_INI_FILE_PATH "/etc/pilot/datalinksim-clouds.ini"

Config::Config() 
{
}

Config& Config::GetInstance() {
	static Config config_;
	return config_;
}

std::string GetFlightSN()
{
	std::fstream fin;
	std::string res;
	fin.open("/etc/pilot/SN", std::ios::in);
	fin >> res;
	fin.close();
	return res;
}
	/**
	* @brief 解析sys_monitor.ini配置文件
	* @param[in] config_path 配置文件路径
	* @return int-0 成功；-1-解析配置文件失败
	*/
int Config::Init(const std::string&config_path) 
{
	cnfig_file_ = CONFIG_INI_FILE_PATH;
	if (config_path.size() > 0) 
	{
		cnfig_file_ = config_path;
	}

	m_dev_sn_ = GetFlightSN();
	int ret = ParseIni();
	if (ret != 0) {
		printf("ini config file %s parse error\n", "/etc/pilot/SN");
		return -1;
	}

	//ret = ParseJson();
	//if (ret != 0) {
	//	printf("json config file %s parse error\n", CONFIG_JSON_FILE_PATH);
	//	return -1;
	//}


	return 0;
}

int Config::ParseIni()
{
	//INI 配置文件解析
	auto ini = std::make_shared<INIReader>(cnfig_file_);
	if (ini->ParseError() < 0)
	{
		printf("Can't load 'config ini\n");
		return -1;
	}

	//日志路径
	m_log_path_ = ini->Get("log", "path", "/media/log/datalink-sim-cloud");

	m_mqtt_ip_ = ini->Get("mqtt", "ip", "127.0.0.1");
	m_sub_topic_ = ini->Get("mqtt", "sub_topic", "/msg/pilot/out");
	m_pub_topic_ = ini->Get("mqtt", "pub_topic", "/msg/pilot/in");
	m_name_ = ini->Get("mqtt", "name", "");
	
	m_key_ = ini->Get("mqtt", "key", "");
	m_cli_id = ini->Get("mqtt", "id", "");
	mqtt_port_ = ini->GetInteger("config", "port", 1883);
	qos_ = ini->GetInteger("config", "qos", 0);

	//tcp_port_ = ini->GetInteger("svr", "tcp_port", 2010);
	//udp_port_ = ini->GetInteger("svr", "udp_port", 2011);

	pilot_sn_ = ini->Get("sn","sn","");
	m_name_ += std::string("-") + pilot_sn_;
	m_cli_id += std::string("-") + pilot_sn_;
	if (pilot_sn_.size() > 0)
	{
		std::string strsn = pilot_sn_ + std::string("/");
		std::size_t pos = m_log_path_.rfind("/");
		if (pos != std::string::npos)
		{
			m_log_path_ = m_log_path_.replace(pos+1, 0, strsn);
		}

		pos = m_sub_topic_.rfind("/");
		if (pos != std::string::npos)
		{
			m_sub_topic_ = m_sub_topic_.replace(pos+1, 0, strsn);
		}

		pos = m_pub_topic_.rfind("/");
		if (pos != std::string::npos)
		{
			m_pub_topic_ = m_pub_topic_.replace(pos+1, 0, strsn);
		}
	}

	//TCP和UDP为空一个端口
	tcp_port_ = GetPortBySN();
	udp_port_ = GetPortBySN();
	return 0;
}

int Config::ParseJson()
{
	//json配置文件获取
	int ret = ReadJsonFile();
	if (ret)
	{
		return ret;
	}
	return 0;
}

int Config::ReadJsonFile()
{
	//std::string str_json_path = CONFIG_JSON_FILE_PATH;
	//bool flag = XFileUtil::is_exist(str_json_path);
	//if (flag == false) {
	//	printf("json file %s not exist\n", str_json_path.c_str());
	//	return -1;
	//}

	//std::string strjson = "";
	//XFile::read_all(str_json_path.c_str(), strjson);

	//if (String2Json(strjson, m_config_json_) == false) 
	//{
	//	printf("json file %s content %s is not json format\n", str_json_path.c_str(), strjson.c_str());
	//	return -1;
	//}
	return 0;
}

/**
 * @brief 根据SN推到出PORT
 * @return port
 */
uint16_t Config::GetPortBySN()
{
	//TCP端口推导规则：由5位10进制数组成，左1位固定2，左第2-5位位SN最后4位的值。最多支持9999架NP206飞机端口分配
    //比如SN:10860999对应的端口位20999
	if (pilot_sn_.size() < 8)
	{
		printf("sn:%s length less than 8", pilot_sn_.c_str());
		return 0;
	}

	size_t pos = pilot_sn_.find("1086"); //NP206 的飞控SN都由1086开头
	if (pos == std::string::npos)
	{
		printf("sn:%s can find 1086", pilot_sn_.c_str());
		return 0;
	}

	std::string strport = pilot_sn_.substr(4, 4); //从第四个字节开始取长度为4个字节的字符串。
	strport = "2" + strport;
	int port = std::atoi(strport.c_str());
	uint16_t sport = port;
	return sport;
}


} //namespace jouav
